package net.hhk.twitter;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;

import net.hhk.controller.TweetAdapter;
import net.hhk.controller.TweetManager;
import net.hhk.data.Constant;
import net.hhk.data.Tweet;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;

public class MyFavoriteActivity extends ListActivity {

	private TweetManager mgr;
	private int id;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_tweet_layout);
		Bundle b = getIntent().getExtras();

		id = b.getInt("id");
		mgr = new TweetManager(getApplicationContext());
		loadData();
		TextView myTweetTitle = (TextView) findViewById(R.id.my_tweet_title);
		myTweetTitle.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(getApplicationContext(),
						MainActivity.class);
				i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
				startActivity(i);
				finish();
			}
		});
		ListView list = getListView();

		LinearLayout write_tweet = (LinearLayout) findViewById(R.id.home_write_tweet);
		write_tweet.setOnClickListener(clickWriteTweet);

		Button btn = (Button) findViewById(R.id.btn_tweet);
		btn.setOnClickListener(clickWriteTweet);
		
		LinearLayout searchLayout = (LinearLayout) findViewById(R.id.btnSearch_layout);
		searchLayout.setOnClickListener(clickSearch);
		Button btnSearch = (Button) findViewById(R.id.btn_search);
		btnSearch.setOnClickListener(clickSearch);

	}

	private OnClickListener clickSearch = new OnClickListener() {

		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent i = new Intent(getApplicationContext(), SearchActivity.class);

			startActivity(i);

		}
	};
	private OnClickListener clickWriteTweet = new OnClickListener() {

		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent i = new Intent(getApplicationContext(),
					WriteTweetActivity.class);
			Bundle b = new Bundle();

			b.putInt("isReply", 0);
			i.putExtras(b);
			startActivity(i);

		}
	};

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		List<Tweet> tweets = mgr.getTweets();
		int tweetid = tweets.get(position).getTweetId();
		String tweetUser = tweets.get(position).getNameRweet();
		Boolean isfavor = tweets.get(position).getIsMyFavorite();
		Boolean isretweet = tweets.get(position).getIsMyRetweet();
		Intent i = new Intent(getApplicationContext(), ViewTweetActivity.class);
		Bundle b = new Bundle();
		b.putInt("id", tweetid);
		b.putString("retweetUser", tweetUser);
		b.putBoolean("isfavorite", isfavor);
		b.putBoolean("isretweet", isretweet);

		// Your id
		i.putExtras(b); // Put your id to your next Inten
		startActivity(i);
	}

	private void loadData() {
		AsyncTask<String, Void, List<Tweet>> asyncTask = new AsyncTask<String, Void, List<Tweet>>() {
			private ProgressDialog progressDialog = new ProgressDialog(
					MyFavoriteActivity.this);

			/*
			 * (non-Javadoc)
			 * 
			 * @see android.os.AsyncTask#onPreExecute()
			 */
			@Override
			protected void onPreExecute() {
				super.onPreExecute();
				progressDialog.setTitle("Loading Tweets");
				if (!progressDialog.isShowing()) {
					progressDialog.show();
				}
			}

			@Override
			protected List<Tweet> doInBackground(String... params) {
				// Following actions will be done in background
				if (id > 0) {
					return mgr.getSocialFeed(Constant.GET_USER_FAVORITE, id, 1,
							6);
				} else
					return mgr
							.getSocialFeed(Constant.GET_MY_FAVORITE, id, 1, 6);
			}

			/*
			 * (non-Javadoc)
			 * 
			 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
			 */
			@Override
			protected void onPostExecute(List<Tweet> result) {
				super.onPostExecute(result);
				if (progressDialog.isShowing()) {
					progressDialog.hide();
				}
				// TweetAdapter listAdapter = new TweetAdapter(
				// getApplicationContext(), result);
				TweetAdapter listAdapter = new TweetAdapter(
						MyFavoriteActivity.this, result);
				setListAdapter(listAdapter);

			}
		};
		asyncTask.execute("Saurabh");
	}
}